package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Common;
import com.example.elderlycare.dao.entity.EquipType;
import com.example.elderlycare.dao.entity.Equipment;
import com.example.elderlycare.dao.mapper.CommonMapper;
import com.example.elderlycare.dao.mapper.EquipTypeMapper;
import com.example.elderlycare.dao.mapper.EquipmentMapper;
import com.example.elderlycare.service.EquipService;
import com.example.elderlycare.service.entity.EquipItem;
import com.example.elderlycare.service.entity.EquipListResponseBody;
import com.example.elderlycare.service.entity.EquipmentExportItem;
import com.example.elderlycare.utils.FormatDate;
import com.example.elderlycare.utils.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ASUS
 */
@Service
public class EquipServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipService {
    @Resource
    EquipmentMapper equipmentMapper;

    @Resource
    CommonMapper commonMapper;

    @Resource
    EquipTypeMapper equipTypeMapper;

    /**
     * queryEquipService 根据参数查询设备信息
     *
     * @param params 查询参数
     * @return EquipListResponseBody
     */
    @Override
    public EquipListResponseBody queryEquipService(Map<String, Object> params) {
        EquipListResponseBody.EquipListResponseBodyBuilder equipListResponseBodyBuilder = EquipListResponseBody.builder();
        int count = equipmentMapper.queryEquipmentCount(params);
        equipListResponseBodyBuilder.total(count);
        List<Equipment> equipments = equipmentMapper.queryEquipmentList(params);
        List<EquipItem> items = new ArrayList<>();
        equipments.forEach((item) -> {
            EquipItem.EquipItemBuilder itemBuilder = EquipItem.builder().
                    id(item.getId()).
                    code(item.getCode()).
                    modelName(item.getModelName()).
                    simCode(item.getSimCode()).
                    issuedAt(FormatDate.getFormatDate(item.getIssuedAt())).
                    isOnline(item.getIsOnline() == 0 ? "在线" : "离线").
                    supplier(item.getSupplier() == null ? "未知" : item.getSupplier().getName()).
                    supplierId(item.getSupplier() == null ? 0 : item.getSupplier().getId()).
                    elderName(item.getElder() == null ? "未知" : item.getElder().getName()).
                    elderId(item.getElder() == null ? 0 : item.getElder().getId()).
                    employee(item.getEmployee() == null ? "未知" : item.getEmployee().getName()).
                    employeeId(item.getEmployee() == null ? 0 : item.getEmployee().getId()).
                    berthId(item.getBerthId() == null ? 0 : item.getBerthId());

            Common issueMethod = commonMapper.selectById(item.getIssueMethodId());
            itemBuilder.issueMethod(issueMethod == null ? "未定义" : issueMethod.getName());
            itemBuilder.issueMethodId(issueMethod == null ? 0 : issueMethod.getId());

            EquipType equipType = equipTypeMapper.selectById(item.getEquipTypeId());
            itemBuilder.equipType(equipType == null ? "未定义" : equipType.getName());
            itemBuilder.equipTypeId(equipType == null ? 0 : equipType.getId());

            Common equipmentStatus = commonMapper.selectById(item.getEquipmentStatusId());
            itemBuilder.equipmentStatus(equipmentStatus == null ? "未定义" : equipmentStatus.getName());
            itemBuilder.equipmentStatusId(equipmentStatus == null ? 0 : equipmentStatus.getId());

            items.add(itemBuilder.build());
        });

        equipListResponseBodyBuilder.items(items);

        return equipListResponseBodyBuilder.build();
    }

    /**
     * exportEquipList 根据参数查询设备信息并导出
     *
     * @param params 查询参数
     */
    @Override
    public void exportEquipList(HttpServletResponse response, Map<String, Object> params) throws IOException {
        List<Equipment> equipments = equipmentMapper.queryEquipmentList(params);
        List<EquipmentExportItem> items = new ArrayList<>();
        equipments.forEach((item) -> {
            EquipmentExportItem.EquipmentExportItemBuilder itemBuilder = EquipmentExportItem.builder().
                    code(item.getCode()).
                    modelName(item.getModelName()).
                    simCode(item.getSimCode()).
                    issuedAt(item.getIssuedAt()).
                    isOnline(item.getIsOnline() == 0 ? "在线" : "离线").
                    supplier(item.getSupplier() == null ? "未知" : item.getSupplier().getName()).
                    employee(item.getEmployee() == null ? "未知" : item.getEmployee().getName());

            Common issueMethod = commonMapper.selectById(item.getIssueMethodId());
            itemBuilder.issueMethod(issueMethod == null ? "未定义" : issueMethod.getName());

            EquipType equipType = equipTypeMapper.selectById(item.getEquipTypeId());
            itemBuilder.equipType(equipType == null ? "未定义" : equipType.getName());

            Common equipmentStatus = commonMapper.selectById(item.getEquipmentStatusId());
            itemBuilder.equipmentStatus(equipmentStatus == null ? "未定义" : equipmentStatus.getName());

            items.add(itemBuilder.build());
        });

        Workbook wb = ExcelUtil.export(items, EquipmentExportItem.class);
        ExcelUtil.writeToResponse(response, wb);
    }
}
